package com.java;

/**
 * 一维数组和二维数组的定义、初始化、长度、遍历
 */
public class Array {
    public static void main(String[] args) {
        // 一维数组静态初始化
        int[] oneStatic1 = new int[]{1, 2, 3};
        int[] oneStatic2 = {1, 2, 3};
        String[] oneStatic3 = new String[]{"A", "B", "C"};
        String[] oneStatic4 = {"A", "B", "C"};
        
        // 一维数组动态初始化
        int[] oneDynamic1 = new int[3];
        String[] oneDynamic2 = new String[3];
        
        // 二维数组静态初始化
        int[][] twoStatic1 = new int[][]{{1, 2, 3}, {4, 5}, {6}};
        int[][] twoStatic2 = {{1, 2, 3}, {4, 5}, {6}};
        String[][] twoStatic3 = new String[][]{{"A", "B", "C"}, {"D", "E"}, {"F"}};
        String[][] twoStatic4 = {{"A", "B", "C"}, {"D", "E"}, {"F"}};
        
        // 二维数组动态初始化
        int[][] twoDynamic1 = new int[3][4];
        int[][] twoDynamic2 = new int[3][];
        String[][] twoDynamic3 = new String[3][4];
        String[][] twoDynamic4 = new String[3][];
        
        // 数组长度
        System.out.println(oneStatic1.length); // 3
        System.out.println(twoStatic1.length); // 3
        System.out.println(twoStatic1[0].length); // 3
        System.out.println(twoStatic1[1].length); // 2
        System.out.println(twoStatic1[2].length); // 1
        
        // 一维数组普通遍历
        for (int i = 0; i < oneStatic1.length; i++) {
            System.out.print(oneStatic1[i] + " ");
        }
        System.out.println();
        
        // 一维数组增强for循环遍历
        for (int a : oneStatic1) {
            System.out.print(a + " ");
        }
        System.out.println();
        
        // 二维数组普通遍历
        for (int i = 0; i < twoStatic1.length; i++) {
            for (int j = 0; j < twoStatic1[i].length; j++) {
                System.out.print(twoStatic1[i][j] + " ");
            }
            System.out.println();
        }
        
        // 二维数组增强for循环遍历
        for (int[] aa : twoStatic1) {
            for (int a : aa) {
                System.out.print(a + " ");
            }
            System.out.println();
        }
    }
}
